package cn.yuan.xiaoyu.wangmodule.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import cn.yuan.xiaoyu.R;

/**
 * Created by yukuoyuan on 2017/3/2.
 * 这是一个我的信息的界面的选择性别的对话框
 */

public class MyInfoCheckSexDialog extends Dialog implements View.OnClickListener {

    private RelativeLayout rl_main_me_myinfo_sex_man;
    private RelativeLayout rl_main_me_myinfo_sex_women;
    private RadioButton rb_main_me_myinfo_sex_man;
    private RadioButton rb_main_me_myinfo_sex_women;
    OnButtonCliickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonCliickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public interface OnButtonCliickListener {
        void onManClick();

        void onWomenClick();
    }

    public MyInfoCheckSexDialog(Context context) {
        this(context, R.style.CustomProgressDialog);
    }

    public MyInfoCheckSexDialog(Context context, int themeResId) {
        super(context, themeResId);
        //设置对话框的显示view
        View view = View.inflate(context, R.layout.dialog_myinfo_check_sex, null);
        this.setContentView(view);
        this.setCancelable(true);
        //设置窗口居中
        this.getWindow().getAttributes().gravity = Gravity.CENTER;
        rl_main_me_myinfo_sex_man = (RelativeLayout) view.findViewById(R.id.rl_main_me_myinfo_sex_man);
        rl_main_me_myinfo_sex_women = (RelativeLayout) view.findViewById(R.id.rl_main_me_myinfo_sex_women);
        rb_main_me_myinfo_sex_man = (RadioButton) view.findViewById(R.id.rb_main_me_myinfo_sex_man);
        rb_main_me_myinfo_sex_women = (RadioButton) view.findViewById(R.id.rb_main_me_myinfo_sex_women);
        rl_main_me_myinfo_sex_man.setOnClickListener(this);
        rl_main_me_myinfo_sex_women.setOnClickListener(this);
        rb_main_me_myinfo_sex_man.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    rb_main_me_myinfo_sex_man.setChecked(true);
                    rb_main_me_myinfo_sex_women.setChecked(false);
                    if (onButtonClickListener != null) {
                        onButtonClickListener.onManClick();
                        dismiss();
                    }
                }

            }
        });
        rb_main_me_myinfo_sex_women.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    rb_main_me_myinfo_sex_man.setChecked(false);
                    rb_main_me_myinfo_sex_women.setChecked(true);
                    if (onButtonClickListener != null) {
                        onButtonClickListener.onWomenClick();
                        dismiss();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_main_me_myinfo_sex_man:
                rb_main_me_myinfo_sex_man.setChecked(true);
                rb_main_me_myinfo_sex_women.setChecked(false);
                if (onButtonClickListener != null) {
                    onButtonClickListener.onWomenClick();
                    dismiss();
                }
                break;
            case R.id.rl_main_me_myinfo_sex_women:
                rb_main_me_myinfo_sex_man.setChecked(false);
                rb_main_me_myinfo_sex_women.setChecked(true);
                if (onButtonClickListener != null) {
                    onButtonClickListener.onManClick();
                    dismiss();
                }
                break;

        }
    }
}
